﻿using System;
using System.Collections.Generic;
using System.Text;
using Chaliy.DbPro.Generators.TextAuthoring.Configuration;

namespace Chaliy.DbPro.Generators.TextAuthoring
{
	public class RandomWordAuthor : IWordAuthor
	{
		#region Fields

		private Random randomSelector;
		private readonly ITermAccessor terms;		

		#endregion

		#region Constructors

		public RandomWordAuthor(ITermAccessor terms)
		{
			if (terms == null)
			{
				throw new ArgumentNullException("terms");
			}
			this.terms = terms;
		}

		#endregion

		#region Properties

		protected Random RandomSelector
		{
			get
			{
				if (this.randomSelector == null)
				{
					this.randomSelector = new Random();
				}
				return this.randomSelector;
			}			
		}

		protected ITermAccessor Terms
		{
			get
			{
				return this.terms; 
			}
		} 


		#endregion

		#region IWordAuthor Members

		public string Generate()
		{
			int termCount = this.Terms.Count;
			if (termCount == 0)
			{
				throw TextAuthorException.CreateSamplesCanNotBeEmpty();
			}

			int nextIndex = this.RandomSelector.Next(0, termCount);
			return this.Terms[nextIndex];			
		}

		#endregion
	}
}
